/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever;

import DataStructures.ProjectInformation;
import DataStructures.ProjectPackage;
import General.Globals;
import General.RequestMaker;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Manuel
 */
public class SFParser extends Parser{

    private String groupID;

    public SFParser(String l){
        super(l);
    }


    protected void retrieveContextualData() {
        data.setUrl(this.link);
        data.setForge(Globals.SOURCEFORGE);
        data.setName(this.parseCharacterData("projects/","/",link,false,true));
        data.setDescription(this.parseCharacterData("<article id=\"project-description\">","</article>",this.currentPage,true,true));
        this.retrieveArchieveLink();
        this.retrieveCollaborators();
        this.retrieveLabels();
        this.retrieveRepositoryLink();
        this.retrieveActivityLevel();
    }

    public void retrieveLabels(){
        String blockString = this.parseCharacterData("<aside id=\"project-secondary\">", "</aside>", this.currentPage, false,true);
        String tagStart = "<section class=\"project-info\">";
        String tagEnd = "</section>";
        String infoTag = "<section class=\"content\">";
        String infoTagEnd = "</a>";
        int startIndex = blockString.indexOf(tagStart);
        while(startIndex != -1){
            String infoBlock = this.parseCharacterData(tagStart, tagEnd, blockString, false,true);
            if(infoBlock.toLowerCase().contains("topic")){
                data.addLabel(this.parseCharacterData(infoTag, infoTagEnd, infoBlock, true,true));
            }

            else if(infoBlock.toLowerCase().contains("language")){
                data.addLanguage(this.parseCharacterData(infoTag, infoTagEnd, infoBlock, true,true));
            }

            blockString = blockString.substring(blockString.indexOf(tagStart)+10);
            startIndex = blockString.indexOf(tagStart);
        }

    }



    private void retrieveCollaborators() {

        //find the user page
        groupID = parseCharacterData("group_id=","\"",this.currentPage,false,true);
        //getting the site which lists the users
        String userPage = RequestMaker.getPage("http://sourceforge.net/project/memberlist.php?group_id="+groupID);

        //check through all links and retrieve user id
        userPage = this.parseCharacterData("</thead>", "</table>", userPage, false,true).toLowerCase();
        int nextLink = userPage.indexOf("<td align=\"middle\">");
        String currentCollaborator;
        while(nextLink != -1){
            userPage = userPage.substring(nextLink);
            currentCollaborator = this.parseCharacterData("<td align=\"middle\">", "</td>",userPage, false,true);
            if(currentCollaborator.contains("/users/")){
                currentCollaborator = this.filterHtml(currentCollaborator);
            data.addCollaboratorIfNew(currentCollaborator);
            }
            nextLink = userPage.indexOf("<td align=\"middle\">",userPage.indexOf("<td align=\"middle\">")+10);
        }
    }


    public void retrieveArchieveLink() {
        //TODO implement
        ProjectPackage pkg = new ProjectPackage();

        //retrieve the package name
        String downloadsPage = RequestMaker.getPage(data.getUrl()+"files");
        String area = this.parseCharacterData("Looking for the latest version?", "class=", downloadsPage, false, true);
        String link = this.parseCharacterData("href=\"", "\"", area, false, true);

        if(link.endsWith("/download"))
            link = link.substring(0,link.lastIndexOf("/"));

        boolean isInterestingLink =  link.endsWith(".tar.bzip2") || link.endsWith(".tbz2") || link.endsWith(".tar.bz2") || link.endsWith(".tar.gzip") || link.endsWith(".tgz") || link.endsWith(".tar.gz") || link.endsWith("zip") || link.endsWith(".tar") || link.endsWith(".jar") || link.endsWith(".class");
        if(link == null || link.isEmpty() || !isInterestingLink)
            return;
        pkg.setName(link);

        //retrieve the date of upload
        String date = parseCharacterData("released on ", " ", area, false, true);
        DateFormat df = DateFormat.getDateInstance();
        Date myDate = new Date();
        if(!date.contains("-"))
            System.out.println("PROBLEM DATE NOT GIVEN: "+ data.getUrl());
        myDate.setYear(Integer.parseInt(date.substring(0,date.indexOf("-")))-1900);
        myDate.setMonth(Integer.parseInt(date.substring(date.indexOf("-")+1,date.lastIndexOf("-"))));
        myDate.setDate(Integer.parseInt(date.substring(date.lastIndexOf("-")+1)));
        pkg.setDate(myDate);
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        if(link == null || link.isEmpty())
            return;
        else
            data.setArchieveLink(pkg);

    }

    public void retrieveRepositoryLink() {
        String url = null;
        String repoPage = RequestMaker.getPage(data.getUrl()+"develop");

        // if it is SVN
        boolean checkout,co = false;
        checkout = repoPage.contains("svn checkout");
        co = repoPage.contains("svn co");
        if(checkout || co){
            if(checkout)
                url = this.parseCharacterData("svn checkout ", " ", repoPage, true, true);
            else if(co)
                url = this.parseCharacterData("svn co ", " ", repoPage, true, true);

            data.setRepositoryType(Globals.SVN);
        }

        //if it is CVS
        if(repoPage.contains("cvs -d")){
            data.setRepositoryType(Globals.CVS);
            //....
        }

        data.setRepositoryLink(url);

    }

    private void retrieveActivityLevel() {
        String appendix = "";
        if(data.getRepositoryType() == Globals.SVN)
            appendix = "&type=svn&mode=60day";
        if(data.getRepositoryType() == Globals.CVS)
            appendix = "&type=cvs&mode=60day";
        
        String statURL = "http://sourceforge.net/project/stats/detail.php?group_id="+this.groupID+"&ugn="+data.getName()+appendix;
        String statPage = RequestMaker.getPage(statURL);
        String statNumber = this.parseCharacterData("<td>Total</td>", "</tr>", statPage, false, true);
        if(statNumber.isEmpty()){
            data.setActivity(0);
            return;
        }
        while(statNumber.contains("<td>")){
            statNumber = statNumber.substring(statNumber.indexOf("<td>")+"<td>".length());
        }
        statNumber = this.filterHtml(statNumber);
        statNumber = statNumber.replaceAll(" ","");
        statNumber = statNumber.replaceAll(",","");
        int stats = Integer.parseInt(statNumber);
        if(stats == 0)
            data.setActivity(0);
        else if(stats < 5)
            data.setActivity(1);
        else if(stats < 30)
            data.setActivity(2);
        else
            data.setActivity(3);
    }

}
